package net.steam.easyjoboa.biz.impl;

import java.util.List;
import net.steam.easyjoboa.biz.LeaveBiz;
import net.steam.easyjoboa.dao.LeaveDao;
import net.steam.easyjoboa.entity.Leave;
import net.steam.easyjoboa.entity.PageBean;

public class LeaveBizImpl implements LeaveBiz {

	private LeaveDao leaveDao;
	
	public void addLeave(Leave leave) throws Exception {
		leaveDao.addEntity(leave);
	}

	public Leave getLeaveById(Integer id) {
		return leaveDao.findById(id);
	}

	public void delLeave(Integer id) throws Exception {
		leaveDao.deleteEntity(id);
	}

	public void setLeaveDao(LeaveDao leaveDao) {
		this.leaveDao = leaveDao;
	}

	public List<Leave> findMyLeaves(PageBean pb,Integer userId,String startDate,String endDate) {
		String hql = "From Leave as l where l.employee.userId = " +userId;
		if(startDate!=null&&endDate!=null&&!startDate.isEmpty()&&!endDate.isEmpty()){
			hql += " and to_char(startDate,'yyyy-mm-dd') = '"+startDate + "' and to_char(endDate,'yyyy-mm-dd') = '"+endDate+"'";
		}
		System.out.println(hql);
		List<Leave> leaves = null;
		leaves = leaveDao.find(pb, hql);
		return leaves;
	}

	public List<Leave> findAllLeaves(PageBean pb,Integer userId,String startDate,String endDate,String userName,String departmentName,String type) {
		String hql = "From Leave where employee.userId != " +userId +" and status = '1' ";
		List<Leave> leaves = null;
		if(startDate!=null&&endDate!=null&&!startDate.isEmpty()&&!endDate.isEmpty()){
			hql += " and to_char(startDate,'yyyy-mm-dd') = '"+startDate + "' and to_char(endDate,'yyyy-mm-dd') = '"+endDate+"'";
		}
		if(type!=null&&!type.isEmpty()){
			hql +=" and type = '"+type+"'";
		}
		if(userName!=null &&!userName.isEmpty()){
			hql +=" and employee.userName = '"+userName+"'";
		}
		if(departmentName !=null&&!departmentName.isEmpty()){
			hql +=" and employee.position.department.departmentName = "+departmentName;
		}
		System.out.println(hql);
		leaves = leaveDao.find(pb, hql);
		return leaves;
	}

	public void updateLeave(Leave leave) {
		leaveDao.updateEntity(leave);
	}
}
